<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="">
	<meta http-equiv="description" content="This is my page">
	
	<script src='${CONTEXT_PATH}/dwr/interface/paperUseService.js'></script>
	<script src='${CONTEXT_PATH}/dwr/engine.js'></script>
	<script src='${CONTEXT_PATH}/dwr/util.js'></script>
	
	<script type="text/javascript" src="${CONTEXT_PATH}/js/login/business/question/define/questionConstant.js"></script>
	
  </head>
  
  <body>
    	<s:form name="frm" id="frm" method="post" action="" namespace="">
    		<jsp:include page="/jsp/outward/business/common/menuLink.jsp">
    			<jsp:param name="menuLink" value="[{'name':'问卷应用'}, {'name':'问卷查错'}]"/>
    			<jsp:param name="width" value="40%"/>
    		</jsp:include>
    		
    		<s:hidden name="paperFix.paper.id" id="paperId"></s:hidden>
    		<s:hidden name="errorMsg" id="errorMsg"></s:hidden>
    		<s:hidden name="keyJson" id="keyJson"></s:hidden>
    		
			<table width="100%" cellspacing="0" cellpadding="3">
    			<tr>
    				<td align="left" valign="bottom" height="30">
    					<input type="button" value="开始查错" class="btn" onclick="beginSearchErrot()" />
    				</td>
    			</tr>
    		</table>
    		
    		<table width="100%" cellspacing="0">
    			<tr>
    				<td width="100px"></td>
    				<td align="left">
    					<div id="qesDiv" style="display:none; width:800px;"></div>
    					<div id="errorMsgDiv" style="display:block-inline; width:800px;"></div>
    				</td>
    			</tr>
    			<tr>
    				<td align="left" colspan="2">
    					<div id="hiddenDiv" style="display:block-inline; width:800px;"></div>
    				</td>
    			</tr>
    			<tr>
    				<td colspan="2" height="50px"></td>
    			</tr>
    		</table>
    	</s:form>
    
    <script type="text/javascript">
	var txtCode = 'qesTxt_';
	var qesHalfQes = {};
	var optionCollect = {};

	function beginSearchErrot()
	{
		qesControlInit();
	}
	
    function searchError()
    {
    	saveFlag = 'error';
    	var errorMsg = $('#errorMsg');
    	var keyJson = $('#keyJson');
    	var errorMsgDiv = $('#errorMsgDiv');
    	errorMsg.val('');
    	var pfList = eval(keyJson.val());
    	var pfLen = pfList.length;
    	for(var x = 0; x < pfLen; x++)
    	{
    		clearTxtValue();
    		errorMsg.val(errorMsg.val() + '<font style="font-weight:bold;">问卷' + pfList[x].pfCode + ':</font><br/>');
    		var key = pfList[x].key;
    		var keyLen = key.length;
    		for(var y = 0; y < keyLen; y++)
    		{
    			var txtId = key[y].txtId;
    			var txtx = $('#' + txtId);
    			txtx.val(key[y].qesKey);
    		}
    		
    		var qesLen = jsonList.length;
	        for(var i = 0; i < qesLen; i++)
			{
				var qesx = jsonList[i];
				if(SINGLE_CHOICE == qesx.qesType)
				{
					checkSaveInfo(qesx.qesCode, '');
				}
				else if(NUMERIC_VALUE == qesx.qesType)
				{
					checkSaveInfo(qesx.qesCode, '');
				}
				else if(MULTIPLE_CHOICE == qesx.qesType)
				{
					checkSaveInfo(qesx.qesCode, '');
				}
				else if(FULL_OPEN == qesx.qesType)
				{
					checkSaveInfo(qesx.qesCode, '');
				}
				else if(GROUP_SINGLE == qesx.qesType)
				{
					var groupQesList = qesx.groupQesList;
	    			var ioLength = groupQesList.length;
	    			for(var j = 0; j < ioLength; j++)
	    			{
						var optionIndex = groupQesList[j].optionValue;
						checkSaveInfo(qesx.qesCode, optionIndex);
	    			}
				}
				else if(GROUP_NUMERIC == qesx.qesType)
				{
					var groupQesList = qesx.groupQesList;
	    			var ioLength = groupQesList.length;
	    			for(var j = 0; j < ioLength; j++)
	    			{
	    				var optionIndex = groupQesList[j].optionValue;
						checkSaveInfo(qesx.qesCode, optionIndex);
	    			}
				}
				else if(GROUP_MULTIPLE == qesx.qesType)
				{
	    			var groupQesList = qesx.groupQesList;
	    			var ioLength = groupQesList.length;
	    			for(var j = 0; j < ioLength; j++)
	    			{
						var optionIndex = groupQesList[j].optionValue;
						checkSaveInfo(qesx.qesCode, optionIndex);
					}
				}
			}
	    	errorMsg.val(errorMsg.val() + '<br/>');
    	}
    	errorMsgDiv.html(errorMsg.val());
    }

    function checkSaveInfo(qesCode, optionIndex)
    {
        try
        {
	    	var errorMsg = $('#errorMsg');
	        var oi = '';
	        if(isNotNull(optionIndex))
	        {
				oi = '_' + optionIndex;
	        }
	        var txtName = txtCode + qesCode + oi;
	        var focusEventHidden = $('#focus_' + qesCode + '_' + optionIndex);
	        var keyEventHidden = $('#key_' + qesCode + '_' + optionIndex);
			var focusFlag = 'I';
			if(isNotNull(focusEventHidden.val()))
			{
				focusFlag = eval(focusEventHidden.val()) == true ? 'Y' : 'N';
			}
			var val = qesHalfQes[qesCode];
	    	var includeFlag = qesInclude(txtName, val);
	    	if('Y' == focusFlag || 'I' == focusFlag)
	    	{
	        	var txtNormal = $('input:text[name="' + txtName + '"][fitType="normal"]');
	        	var normalLen = txtNormal.length;
	        	var normalFlag = false;
	        	var openFlag = false;
	        	var firstNormal = null;
	        	for(var i = 0; i < normalLen; i++)
	        	{
	            	var normalx = $(txtNormal.get(i));
	            	if(i == 0)
	            	{
	            		firstNormal = normalx;
	               	}
	            	if((isNotNull(normalx.val()) && 'Y' == focusFlag) || 'I' == focusFlag)
	            	{
	            		normalFlag = true;
	                }
	                if(!hasOption(normalx.attr('id'), normalx.val()))
	                {
	                	errorMsg.val(errorMsg.val() + qesCode + '没有选项' + normalx.val() + '。; ');
	                }
	        	}
	        	if(normalFlag == false)
	            {
	        		var subQes = '';
			    	if(isNotNull(optionIndex))
			    	{
			    		subQes = '子题' + optionIndex;
				    }
	                errorMsg.val(errorMsg.val() + '请填写' + qesCode + subQes + '的答案。; ');
	            }
	        	if(includeFlag)
	        	{
	        		var txtOpen = $('input:text[name="' + txtName + '"][fitType="open"]');
	        		var openLen = txtOpen.length;
	        		var firstOpen = null;
	        		for(var i = 0; i < openLen; i++)
	            	{
	                	var openx = $(txtOpen.get(i));
	                	if(i == 0)
	                	{
	                		firstOpen = openx;
	                   	}
	                	if(isNotNull(openx.val()))
	                	{
	               			openFlag = true;
	                    }
	            	}
	                if(openFlag == false)
	                {
	                	var subQes = '';
				    	if(isNotNull(optionIndex))
				    	{
				    		subQes = '子题' + optionIndex;
					    }
	                	errorMsg.val(errorMsg.val() + '请填写' + qesCode + subQes + '的半开放题答案。; ');
	                }
	        	}
	        }
	    	else if('N' == focusFlag)
	    	{
		    	var thisQes = getQuestion(qesCode, optionIndex);
		    	var notNullValues = thisQes.notNullValues();
		    	if(notNullValues.length > 0)
		    	{
			    	var subQes = '';
			    	if(isNotNull(optionIndex))
			    	{
			    		subQes = '子题' + optionIndex;
				    }
		    		errorMsg.val(errorMsg.val() + '请去除' + qesCode + subQes + '的选项。; ');
			    }
		    }
	
	        eval(keyEventHidden.val());
        }
        catch(e)
        {
			alert(e.message);
		}
    }
    
    function qesControlInit()
    {
    	showLoading('正在查错，请稍候...');
    	var qesDiv = $('#qesDiv');
    	var errorMsgDiv = $('#errorMsgDiv');
    	qesDiv.html('');
    	errorMsgDiv.html('');
    	var url = 'inner/qes/questionListByPaper.action';
    	ajaxAction(url, 'post', 'frm', true, function(obj){
        	var hiddenDiv = $('#hiddenDiv');
        	hiddenDiv.html('');
        	var eventHiddenHtml = '';
    		openQesWidth = 80;
    		jsonList = obj.qesList;
    		var jsonPaper = eval(obj.jsonPaperStr);
    		var halfQesRow = jsonPaper[0].halfQesRow;
    		var fullQesRow = jsonPaper[0].fullQesRow;
    		var len = jsonList.length;
    		var html = '<table id="qesTable" width="100%" cellpadding="5" cellspacing="1" bgcolor="#CCCCCC">';
    		var level = 10;
    		for(var i = 0; i < len; i++)
    		{
    			var qesx = jsonList[i];
    			var trClass = 'class="tableRow1"';
    			if(i % 2 == 1)
    			{
    				trClass = 'class="tableRow2"';
    			}
    			html += '<tr bgcolor="#FFFFFF" ' + trClass + '><td align="left" id="td_' + qesx.qesCode + '">';
    			if(SINGLE_CHOICE == qesx.qesType)
    			{
	    			html += qesx.qesCode + '.' + qesx.qesTitle + ': ';
	    			var txtName = txtCode + qesx.qesCode;
	    			var txtId = txtCode + qesx.qesCode + '_sc_';
   					html += '<input fitType="normal" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (qesx.width * level) + 'px;" maxlength="' + qesx.width + '" codeTable="' + qesx.codeTable.id + '" /> ';
   					if(qesx.codeTable != null && 'Y' == qesx.codeTable.hasHalfQes)
   					{
   						html += '<br/>其他文本: ';
   						for(var j = 0; j < halfQesRow; j++)
   						{
   							var txtName = txtCode + qesx.qesCode;
   			    			var txtId = txtCode + qesx.qesCode + '_sct_' + j;
   							html += '<br/> <input fitType="open" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (openQesWidth * level) + 'px;" maxlength="' + openQesWidth + '" />';
   	   					}
   					}
   					addOptionToCollect(qesx.qesCode, qesx.optionList);
   					addHalfQesToCollect(qesx);
   					eventHiddenHtml += eventHiddenStr(qesx.qesCode, qesx.focusEvent, qesx.keyEvent, qesx.qesType);
    			}
    			else if(NUMERIC_VALUE == qesx.qesType)
    			{
    				html += qesx.qesCode + '.' + qesx.qesTitle + ': ';
    				var txtName = txtCode + qesx.qesCode;
		    		var txtId = txtCode + qesx.qesCode + '_nv_';
   					html += '<input fitType="normal" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (qesx.width * level) + 'px;" maxlength="' + qesx.width + '" /> ';
   					eventHiddenHtml += eventHiddenStr(qesx.qesCode, qesx.focusEvent, qesx.keyEvent, qesx.qesType);
    			}
    			else if(MULTIPLE_CHOICE == qesx.qesType)
    			{
	    			html += qesx.qesCode + '.' + qesx.qesTitle + ': ';
	    			for(var j = 0; j < qesx.quantity; j++)
	    			{
	    				var txtName = txtCode + qesx.qesCode;
			    		var txtId = txtCode + qesx.qesCode + '_mc_' + j;
    					html += '<input fitType="normal" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (qesx.width * level) + 'px;" maxlength="' + qesx.width + '" codeTable="' + qesx.codeTable.id + '" /> ';
    				}
    				if(qesx.codeTable != null && 'Y' == qesx.codeTable.hasHalfQes)
   					{
   						html += '<br/>其他文本: ';
   						for(var j = 0; j < halfQesRow; j++)
   						{
   							var txtName = txtCode + qesx.qesCode;
   				    		var txtId = txtCode + qesx.qesCode + '_mct_' + j;
   							html += '<br/> <input fitType="open" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (openQesWidth * level) + 'px;" maxlength="' + openQesWidth + '" />';
   						}
   					}
   					addOptionToCollect(qesx.qesCode, qesx.optionList);
   					addHalfQesToCollect(qesx);
    				eventHiddenHtml += eventHiddenStr(qesx.qesCode, qesx.focusEvent, qesx.keyEvent, qesx.qesType);
    			}
    			else if(FULL_OPEN == qesx.qesType)
    			{
    				html += qesx.qesCode + '.' + qesx.qesTitle + ': ';
    				for(var j = 0; j < fullQesRow; j++)
    				{
    					var txtName = txtCode + qesx.qesCode;
			    		var txtId = txtCode + qesx.qesCode + '_fo_' + j;
   						html += '<br/> <input fitType="normal" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (openQesWidth * level) + 'px;" maxlength="' + openQesWidth + '" />';
   					}
   					eventHiddenHtml += eventHiddenStr(qesx.qesCode, qesx.focusEvent, qesx.keyEvent, qesx.qesType);
    			}
    			else if(GROUP_SINGLE == qesx.qesType)
    			{
	    			html += qesx.qesCode + '.' + qesx.qesTitle + ': <br/>';
	    			var groupQesList = qesx.groupQesList;
	    			var ioLength = groupQesList.length;
	    			for(var j = 0; j < ioLength; j++)
	    			{
    					if(j >= 1)
    					{
    						html += '<br/>';
    					}
    					var optionIndex = groupQesList[j].optionValue;
	    				html += optionIndex + '.' + groupQesList[j].description + ': ';
	    				var txtName = txtCode + qesx.qesCode + '_' + optionIndex;
			    		var txtId = txtCode + qesx.qesCode + '_gs_' + optionIndex;
    					html += '<input fitType="normal" subQes="' + optionIndex + '" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (qesx.width * level) + 'px;" maxlength="' + qesx.width + '" codeTable="' + qesx.codeTable.id + '" />';
    					if(qesx.codeTable != null && 'Y' == qesx.codeTable.hasHalfQes)
    					{
    						html += '<br/>其他文本: ';
    						for(var k = 0; k < halfQesRow; k++)
    						{
    							var txtName = txtCode + qesx.qesCode + '_' + optionIndex;
    				    		var txtId = txtName + '_gst_' + optionIndex + '_' + k;
    							html += '<br/> <input fitType="open" type="text" id="' + txtId + '" name="' + txtName + '" onfocus="qesFocus(\'' + txtId + '\', \'' + optionIndex + '\')" onkeyup="qesKeyup(\'' + txtId + '\', \'' + optionIndex + '\')" style="width:' + (openQesWidth * level) + 'px;" maxlength="' + openQesWidth + '" />';
    						}
    					}
    					eventHiddenHtml += eventHiddenStr(qesx.qesCode, groupQesList[j].focusEvent, groupQesList[j].keyEvent, qesx.qesType, optionIndex);
        			}
        			addOptionToCollect(qesx.qesCode, qesx.optionList);
        			addHalfQesToCollect(qesx);
    			}
    			else if(GROUP_NUMERIC == qesx.qesType)
    			{
	    			html += qesx.qesCode + '.' + qesx.qesTitle + ': <br/>';
	    			var groupQesList = qesx.groupQesList;
	    			var ioLength = groupQesList.length;
	    			for(var j = 0; j < ioLength; j++)
	    			{
	    				if(j >= 1)
    					{
    						html += '<br/>';
    					}
    					var optionIndex = groupQesList[j].optionValue;
	    				html += optionIndex + '.' + groupQesList[j].description + ': ';
	    				var txtName = txtCode + qesx.qesCode + '_' + optionIndex;
			    		var txtId = txtCode + qesx.qesCode + '_gn_' + optionIndex;
    					html += '<input fitType="normal" subQes="' + optionIndex + '" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (qesx.width * level) + 'px;" maxlength="' + qesx.width + '" />';
    					eventHiddenHtml += eventHiddenStr(qesx.qesCode, groupQesList[j].focusEvent, groupQesList[j].keyEvent, qesx.qesType, optionIndex);
    				}
    			}
    			else if(GROUP_MULTIPLE == qesx.qesType)
    			{
	    			html += qesx.qesCode + '.' + qesx.qesTitle + ': <br/>';
	    			var groupQesList = qesx.groupQesList;
	    			var ioLength = groupQesList.length;
	    			for(var j = 0; j < ioLength; j++)
	    			{
	    				if(j >= 1)
    					{
    						html += '<br/>';
    					}
    					var optionIndex = groupQesList[j].optionValue;
	    				html += optionIndex + '.' + groupQesList[j].description + ': ';
	    				for(var k = 0; k < qesx.quantity; k++)
	    				{
	    					var txtName = txtCode + qesx.qesCode + '_' + optionIndex;
				    		var txtId = txtCode + qesx.qesCode + '_gm_' + optionIndex + '_' + k;
    						html += '<input fitType="normal" subQes="' + optionIndex + '" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (qesx.width * level) + 'px;" maxlength="' + qesx.width + '" codeTable="' + qesx.codeTable.id + '" /> ';
    					}
    					if(qesx.codeTable != null && 'Y' == qesx.codeTable.hasHalfQes)
    					{
    						html += '<br/>其他文本: ';
    						for(var k = 0; k < halfQesRow; k++)
    						{
    							var txtName = txtCode + qesx.qesCode + '_' + optionIndex;
    				    		var txtId = txtName + '_gmt_' + optionIndex + '_' + k;
    							html += '<br/> <input fitType="open" type="text" id="' + txtId + '" name="' + txtName + '" style="width:' + (openQesWidth * level) + 'px;" maxlength="' + openQesWidth + '" />';
    						}
    					}
    					eventHiddenHtml += eventHiddenStr(qesx.qesCode, groupQesList[j].focusEvent, groupQesList[j].keyEvent, qesx.qesType, optionIndex);
    				}
    				addOptionToCollect(qesx.qesCode, qesx.optionList);
   					addHalfQesToCollect(qesx);
    			}
    			html += '</td></tr>';
    		}
    		html += '</table>';
    		qesDiv.html(html);
    		hiddenDiv.html(eventHiddenHtml);
    		
    		var paperId = $('#paperId');
    		paperUseService.queryAllPaperFixKeyJson(paperId.val(), function(data){
	    		var keyJson = $('#keyJson');
	    		keyJson.val(data);
	    		//查错
	    		searchError();
    			
	    		hideLoading();
    		});
    	});
    }
    
    function hasOption(txtId, value)
    {
        var txtx = $('#' + txtId);
        var qesCode = txtx.attr('name').split('_')[1];
        var arr = optionCollect[qesCode];
        if(arr == null)
        {
            return true;
        }
        var len = arr.length;
        for(var i = 0; i < len; i++)
        {
            if(!isNotNull(value)
                    || arr[i].optionValue == value 
                    || txtId.indexOf('_sct_') > -1 
                    || txtId.indexOf('_mct_') > -1 
                    || txtId.indexOf('_gst_') > -1 
                    || txtId.indexOf('_gmt_') > -1)
            {
                return true;
            }
        }
        return false;
    }
    
    function addOptionToCollect(qesCode, list)
    {
        var len = list.length;
        var arr = new Array();
        for(var i = 0; i < len; i++)
        {
            var obj = {'optionValue':list[i].optionValue, 'description':list[i].description};
            arr.push(obj);
        }
        optionCollect[qesCode] = arr;
    }
    
    function qesInclude(txtName, val)
    {
    	var qesTxt = $('input:text[name="' + txtName + '"][fitType="normal"]');
    	var len = qesTxt.length;
    	for(var i = 0; i < len; i++)
    	{
    		var txtx = $(qesTxt.get(i));
    		if(isNotNull(txtx.val()) && txtx.val() == val)
    		{
    			return true;
    		}
    	}
    	return false;
    }

    function eventHiddenStr(qesCode, focusEvent, keyEvent, qesType, optionIndex)
    {
    	if(isNotNull(optionIndex) == false)
    	{
    		optionIndex = '';
    	}
    	if(isNotNull(focusEvent) == false)
    	{
    		focusEvent = 'true;';
    	}
    	if(isNotNull(keyEvent) == false)
    	{
    		keyEvent = '';
    	}
        var focusHidden = '<input name="focusHiddenEvents" type="hidden" id="focus_' + qesCode + '_' + optionIndex + '" value="' + focusEvent + '" qesType="' + qesType + '" />';
        focusHidden += '<input name="focusBakHiddenEvents" type="hidden" id="focusBak_' + qesCode + '_' + optionIndex + '" value="' + focusEvent + '" qesType="' + qesType + '" />';
        var keyHidden = '<input name="keyHiddenEvents" type="hidden" id="key_' + qesCode + '_' + optionIndex + '" value="' + keyEvent + '" qesType="' + qesType + '" />';
        return focusHidden + keyHidden;
    }
    
    function clearTxtValue()
    {
    	var allTxt = $('#qesDiv input:text');
    	var len = allTxt.length;
    	for(var i = 0; i < len; i++)
    	{
    		var txtx = $(allTxt.get(i));
    		txtx.val('');
    	}
    }
    
    function addHalfQesToCollect(qesx)
    {
    	var value = qesx.codeTable.halfQesVal;
    	qesHalfQes[qesx.qesCode] = value;
    }
    </script>
    
    <script type="text/javascript" src="${CONTEXT_PATH}/js/login/business/question/use/events.js"></script>
  </body>
</html>
